/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright  Copyright (c) 2006-2007, Julien PORTALIER
 * @link       http://feather-cm.googlecode.com/
 * @license    http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

_gettext_add_translations("fr",
{
	'spam':     'spam',
	'troll':    'troll',
	'not spam': 'non spam',
	'not troll': 'non troll',
	'Loading comment...': "Chargement du commentaire...",
	'Are you sure you want to delete this comment?': "Êtes-vous certain de vouloir effacer ce commentaire?",
	'Comment deleted': 'Commentaire effacé',
	'Comment now considered as spam':        "Le commentaire est maintenant considéré comme spam",
	'Comment now considered as troll':       "Le commentaire est maintenant considéré comme troll",
	'Comment no longer considered as spam':  "Le commentaire n'est plus considéré comme spam",
	'Comment no longer considered as troll': "Le commentaire n'est plus considéré comme troll"
});

var Comments = new Class(
{
	initialize: function()
	{
		this.a    = {};
		this.ajax = {};
		
		// spams & trolls are hidden
		$ES('.comments dt .spam').addEvent('click',  this.toggleHidden.bindWithEvent(this));
		$ES('.comments dt .troll').addEvent('click', this.toggleHidden.bindWithEvent(this));
		
		// admin
		$ES('.comments dd').each(function(dd) { this.adminEvents(dd); }, this);
	},
	
	toggleHidden: function(evt)
	{
		var a   = evt.target;
		var dt  = $(a.parentNode);
		var dd  = dt.getNext();
		
		if (!dd || dd.getTag() != 'dd')
		{
			dd = new Element('dd');
			dd.appendChild(new Element('p', {'class': 'loading'}).setText(_('Loading comment...')));
			dd.injectAfter(dt);
			
			new Ajax(a.getAttribute('href'), {
				'method': 'get',
				'update': dd,
				'onComplete': this.adminEvents.pass(dd, this)
			}).request();
			
			a.setText('[-]');
		}
		else if (dd.getStyle('display') == 'none')
		{
			dd.setStyle('display', '');
			a.setText('[-]');
		}
		else
		{
			dd.setStyle('display', 'none');
			a.setText('[+]');
		}
		
		evt.stop();
	},
	
	adminEvents: function(dd)
	{
		$ES('.infos .admin .delete', dd).addEvent('click', this.onDelete.bindWithEvent(this));
		$ES('.infos .admin .spam', dd).addEvent('click', this.onAction.bindWithEvent(this));
		$ES('.infos .admin .troll', dd).addEvent('click', this.onAction.bindWithEvent(this));
	},
	
	
	// deletion
	
	onDelete: function(evt)
	{
		var a = $(evt.target);
		if (!a.hasClass('waiting')) 
		{
			if (confirm(_('Are you sure you want to delete this comment?'))) 
			{
				a.addClass('waiting');
				new Ajax(a.getAttribute('href'), {
					'method': 'get',
					'onFailure': this.failure.bind(this),
					'onSuccess': this.onDeleteSuccess.pass(a, this)
				}).request();
			}
		}
		evt.stop();
	},
	
	onDeleteSuccess: function(a)
	{
		// remove comment
		var dd = a;
		while (dd && dd.getTag() != 'dd') 
		{
			if (!dd.parentNode)
				return;
			dd = $(dd.parentNode);
		}
		dd.getPrevious().remove();
		dd.remove();
		
		// message user
		alert(_('Comment deleted'));
	},
	
	
	// spams & trolls
	
	onAction: function(evt)
	{
		var a = $(evt.target);
		if (!a.hasClass('waiting')) 
		{
			a.addClass('waiting');
			new Ajax(a.getAttribute('href'), {
				'method': 'get',
				'onFailure': this.failure.bind(this),
				'onSuccess': this.afterAction.pass(a, this)
			}).request();
		}
		evt.stop();
	},
	
	afterAction: function(a)
	{
		a.removeClass('waiting');
		var type = a.getAttribute('class');
		if (a.getText() == _(type))
		{
			a.setText(_('not ' + type));
			alert(_('Comment now considered as ' + type));
		}
		else
		{
			a.setText(_(type));
			alert(_('Comment no longer considered as ' + type));
		}
	},
	
	
	// generic
	
	failure: function(txt)
	{
		alert(txt);
	}
});

window.addEvent('domready', function() { new Comments(); });